# -*- encoding: utf-8 -*-
# @Time       :  18:28
# @Author     : yuxian
# @Email      : 1503889663@qq.com
# @File       : rewrite_login.py
# @SoftWare   : PyCharm
from django_redis import get_redis_connection


def jwt_token_use(token, user=None, request=None):
    '''自定义登录成功返回 数据函数'''
    uuid = request.data.get('uuid')
    verify_code = request.data.get('verify')
    if not uuid or not verify_code:
        if not uuid:
            return {'msg': 'param error'}
        return {"msg": '请输入验证码'}
    cache = get_redis_connection(alias='verify_code')
    redis_verify_code = cache.get("login:verification:%s" % uuid)
    cache.delete("login:verification:%s" % uuid)
    if not redis_verify_code:
        return {"msg": '验证码过期'}

    if redis_verify_code.upper() != verify_code.upper():
        return {'msg': '验证码输入错误'}
    data = {
        'token': token,
        'id': user.id,
        'username': user.username,
    }
    return data